<?php 
include_once 'viper/autoloader.php';

class viper_client_user
{
	private $_userId;
	
	private $_featureScores; //caries sorted by scores. features are the keys
	private $_friends;
	private $_recommendedFeatures;
	
	public function __construct($uid){
		$this->_userId = $uid;
		$this->_loadFeatureScores();
		$this->_loadFriends();
		$this->_loadRecommendedFeatures();
	}
	
	private function _loadFeatureScores(){
		$sql = 'SELECT * FROM userfeaturescore WHERE userid=' . $this->_userId;
		$result = viper_dao::query($sql);
		
		if($result){
			$result = $result[0];
		} else {
			die('Failed to load feature scores');
		}
		//get rid of userid as first column
		array_shift($result);
		
		$this->_featureScores = $result;
	}
	
	private function _loadFriends(){
		$sql = 	'SELECT * FROM userquicklist WHERE userid=' . $this->_userId;
		$result = viper_dao::query($sql);
		
		if($result){
			$result = $result[0];
		} else {
			die('Failed to load friends');
		}
		//get rid of userid as first column
		array_shift($result);
		
		$this->_friends = $result;
	}
	
	private function _loadRecommendedFeatures(){
		$sql = 'SELECT * FROM globalaverage';
		$globalaverage = viper_dao::query($sql);
		$globalaverage = $globalaverage[0];
		
		$scores = array();
		for($i=0;$i<50;$i++){
			$scores['feature'.$i] = 0;
		}
		
		foreach($this->_friends as $friend){
			if($friend == -1){
				for($i=0;$i<50;$i++){
					$scores['feature'.$i] = $scores['feature'.$i] + ($globalaverage['feature'.$i]/100); 
				}
			} else {
				$sql = 'SELECT * FROM userfeaturescore WHERE userid=' . $friend;
				$result = viper_dao::query($sql);
				$result = $result[0];
				
						for($i=0;$i<50;$i++){
					$scores['feature'.$i] = $scores['feature'.$i] + $result['feature'.$i]; 
				}
			}
		}
		
		asort($scores);
		$scores = array_reverse($scores, true);
		
		$this->_recommendedFeatures = $scores;
	}
	
	public function setFeatureScores($scores){
		//updates userfeaturescore
		$sql = 'UPDATE userfeaturescore SET feature0=' . $scores['feature0'];
		foreach($scores as $key => $value){
			$sql = $sql . ',' . $key . '=' . $value;
		}
		$sql = $sql . ' WHERE userid=' . $this->_userId;
		
		$result = viper_dao::query($sql);
		
		if(!$result){
			die ('Could not update user scores');
		}
		
		//updates globalaverage
		$sql = 'SELECT * FROM globalaverage';
		$globalaverage = viper_dao::query($sql);
		$globalaverage = $globalaverage[0];
		
		$sql = 'UPDATE globalaverage SET feature0=' . $globalaverage['feature0'];
		
		foreach($globalaverage as $key => $value){
			$globalaverage[$key] = $value + $scores[$key] - $this->_featureScores[$key];
			
			$sql = $sql . ',' . $key . '=' . $globalaverage[$key];
		}
		
		$result = viper_dao::query($sql);
		
		if(!$result){
			die ('Could not update global averages');
		}
		
		$this->_featureScores = $scores;
	}
	
	public function getFeatureScores(){
		return $this->_featureScores;
	}
	
	public function getFriends(){
		return $this->_friends;
	}
	
	public function getRecommendedFeatureList(){
		return array_keys($this->_recommendedFeatures);
	}
	
	public function getRecommendedFeatureScores(){
		return $this->_recommendedFeatures;
	}
}
?>